﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nhandang
{
    class MakeImageBinary
    {
        public static string ImageBinary(string path,string class_)
        {
            string chuoi="";
            CvSeq[] contour = new CvSeq[1]; // dùng để theo doi 1 đối tượng //http://iamabhik.wordpress.com/category/opencv/
            CvSeq contourLow;
            IplImage imagen = cvlib.cvLoadImage(path, 0);
            IplImage imagen_color = cvlib.cvLoadImage(path, 1);
            /*imagen= cvLoadImage("so0.png",0);
            imagen_color=cvLoadImage("so0.png",1);*/

            //Create needed images
            IplImage smooth = cvlib.cvCreateImage(cvlib.cvSize(imagen.width, imagen.height), cvlib.IPL_DEPTH_8U, 1);
            IplImage threshold = cvlib.cvCreateImage(cvlib.cvSize(imagen.width, imagen.height), cvlib.IPL_DEPTH_8U, 1);
            IplImage open_morf = cvlib.cvCreateImage(cvlib.cvSize(imagen.width, imagen.height), cvlib.IPL_DEPTH_8U, 1);
            // tgray = cvlib.cvCreateImage(cvlib.cvSize(imagen.width, imagen.height), cvlib.IPL_DEPTH_8U, 1);
            //Init variables for countours
            contour[0] = new CvSeq();
            // contourLow = 0;
            //Create storage needed for contour detection
            CvMemStorage storage = cvlib.cvCreateMemStorage(0);
            cvlib.cvSmooth(imagen, smooth, cvlib.CV_GAUSSIAN, 3, 0, 0, 0);
            CvScalar avg = new CvScalar();
            CvScalar avgStd = new CvScalar();
            cvlib.cvAvgSdv(smooth, avg, avgStd);
            cvlib.cvThreshold(smooth, threshold, (int)avg.Val[0] - 7 * (int)(avgStd.Val[0] / 8), 255, cvlib.CV_THRESH_BINARY_INV);
            //Morfologic filters
            IplConvKernel k = cvlib.cvCreateStructuringElementEx(3, 3, 1, 1, cvlib.CV_SHAPE_RECT, IntPtr.Zero);
            cvlib.cvErode(threshold, open_morf, k, 1);// gian~
            cvlib.cvDilate(open_morf, open_morf, k, 1); // an mon`
            //Duplicate image for countour
            IplImage img_contornos = cvlib.cvCloneImage(open_morf);
            //Search countours in preprocesed image
            cvlib.cvFindContours(img_contornos, storage, contour, 88, cvlib.CV_RETR_EXTERNAL, cvlib.CV_CHAIN_APPROX_SIMPLE, cvlib.cvPoint(0, 0));
            //Optimize contours, reduce points
            contourLow = cvlib.cvApproxPoly(CvSeq.getCPtr(contour[0]).Handle, 88, storage, cvlib.CV_POLY_APPROX_DP, 1, 1);
            //int i =0;
            Random rd = new Random();
          //  for (int i = 0; contourLow != null; contourLow = contourLow.h_next)
            {
                CvScalar color = cvlib.CV_RGB(rd.Next(200), rd.Next(200), rd.Next(200));
                //We can draw the contour of object
                //cvDrawContours( imagen_color, contourLow, color, color, 1, 0, 8, cvPoint(0,0) );
                //Or detect bounding rect of contour	
                CvRect rect;
                CvPoint pt1 = new CvPoint();
                CvPoint pt2 = new CvPoint();
                rect = cvlib.cvBoundingRect(contourLow, 0);//
                pt1.x = rect.x;
                pt2.x = (rect.x + rect.width);
                pt1.y = rect.y;
                pt2.y = (rect.y + rect.height);
                cvlib.cvRectangle(imagen_color, pt1, pt2, color, 1, 8, 0);//ve hinh chu nhat bao quanh doi tuong
                IplImage anh;
                anh = cvlib.cvCreateImage(cvlib.cvSize(16, 16), open_morf.depth, open_morf.nChannels);
                //Crop image
                //string s1 = "anh"+i.ToString();
                //cvlib.cvNamedWindow(s1, 0);
                cvlib.cvSetImageROI(open_morf, rect);//crop hinh

                //cvlib.cvCopy(imagen_color, anh);
                cvlib.cvResize(open_morf, anh);

                for (int rong = 0; rong < anh.width; rong++)
                {
                    for (int dai = 0; dai < anh.height; dai++)
                    {
                        CvScalar s = cvlib.cvGet2D(anh, rong, dai);
                        // MessageBox.Show(rong.ToString()+" "+dai.ToString()+" "+s.Val[0].ToString());
                        if (s.Val[0].ToString() == "255")
                            chuoi = chuoi + "0, ";
                        else
                            chuoi = chuoi + "1, ";
                    }
                }
                                
           }
            return chuoi = chuoi + class_;
        }
    }
}
